package com.example.ad_project;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.NumberPicker;
import android.widget.TextView;

import com.nus.iss.ad.dbhelper.DiscrepancyDBHelper;
import com.nus.iss.ad.model.DiscrepancyReport;
import com.nus.iss.ad.model.Inventory;
import com.nus.iss.ad.model.LoginUserInfo;

public class RaiseDiscrepancyDetailActivity extends BaseActivity implements OnClickListener{
    Inventory inv;
    TextView tvCode,tvDescription,tvQuantity,tvEmpName;
    Button b;
    NumberPicker np;
    final int minValue = -500;
    final int maxValue = 500;
    EditText reason;
    DiscrepancyDBHelper data=new DiscrepancyDBHelper();

    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_raise_discrepancy_detail);
		tvCode=(TextView)findViewById(R.id.tvCode);
		tvDescription=(TextView)findViewById(R.id.tvDescription);
		tvQuantity=(TextView)findViewById(R.id.tvQuantity);
		tvEmpName=(TextView)findViewById(R.id.tvEmpName);
		reason = (EditText) findViewById(R.id.editTextReason);
		
		Bundle extras = getIntent().getExtras();
        if (extras == null)
          return;
        String itemCode = extras.getString("code");
        inv=data.getInventoryById(itemCode);
        tvCode.setText(itemCode);
        tvDescription.setText(inv.getDescription().toString());
        tvQuantity.setText(Integer.toString(inv.getStock()));
        tvEmpName.setText(LoginUserInfo.currentUser.getName());
        
        np=(NumberPicker)findViewById(R.id.numberPicker1);
        np.setMinValue(0);
        np.setMaxValue(maxValue - minValue);
        np.setValue(0 - minValue);
        np.setFormatter(new NumberPicker.Formatter() {
            @Override
            public String format(int index) {
                return Integer.toString(index - maxValue);
            }
        });
        
        
        b=(Button)findViewById(R.id.btnReport);
        b.setOnClickListener(this);
	}

	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		int value=np.getValue()+minValue;
		if(value==0){
			showDialog("Invalid Value", "Discrepancy value cannot be zero", false);
		}
		else{
		DiscrepancyReport dis=new DiscrepancyReport();
		dis.setInventoryId(tvCode.getText().toString());
		dis.setAdjAmt(value);
		dis.setReason(reason.getText().toString());
		data.reportDiscrepancy(dis);
		
		showDialog("Discrepancy Report", "Report Successfully", true);

	}
  }
}
